package 
{
	import com.buraks.utils.fastmem;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.system.System;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author Guillaume Sylvain
	 */
	public class Main extends Sprite 
	{
		private var resultTxt:TextField = null;
		private var resultString:String = "";
		private var start_time:Number = 0;
		private var end_time:Number = 0;
		private var start_mem:uint = 0;
		private var end_mem:uint = 0;
		private var i:int = 0;
		private var len:int = 10000000;
		private var resultNumber:Number = 0;
		private var testList:Array = [];
		private var mVector:Vector.<Number> = new Vector.<Number>();
		private var mVector2:Vector.<Number> = new Vector.<Number>();
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(aEvent:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			//AccessTime();
			//NullCheck();
			//LoopOnVectorLength();
			LoopWithIntUint();
			/**************************/
Killer Random

const MAX_RATIO:Number = 1 / uint.MAX_VALUE;
var r:uint = Math.random() * uint.MAX_VALUE;

//returns a number from 0 - 1
function XORandom():Number
{
	r ^= (r << 21);
	r ^= (r >>> 35);
	r ^= (r << 4);
	return (r * MAX_RATIO);
}

/**************************/

Call de fonction au lieu de dispatchEvent / addEventListener

Dispatch event - 500 000 itérations: 1816 ms, 8228 ko
Function call  - 500 000 itérations: 25 ms, 0 ko

/**************************/
			DisplayResult();
		}
		
		private function Bench():void
		{
			ToTrace("Bench");
			StartRecordingTime();
			
			for (i = 0; i < len; i++)
			{
				
			}
			
			StopRecordingTime();
		}
		
		private function StartRecordingTime():void
		{
			start_mem = System.totalMemory / 1024;
			start_time = getTimer();
		}
		
		private function StopRecordingTime():void
		{
			end_time = getTimer();
			end_mem = System.totalMemory / 1024;
			
			ToTrace(String(end_time - start_time) + " ms, " + String(end_mem - start_mem) + " ko");
		}
		
		private function LoopOnVectorLength():void
		{
			var fixVec:Vector.<int> = new Vector.<int>(len, false);
			var dynVec:Vector.<int> = new Vector.<int>(len, true);
			
			ToTrace("LoopOnVectorLength - fixed"); // 65 ms with 10M
			StartRecordingTime();
			
			for (i = 0; i < fixVec.length; i++)
			{
				
			}
			
			StopRecordingTime();
			
			//
			
			ToTrace("LoopOnVectorLength - dynamic"); // 65 ms with 10M
			StartRecordingTime();
			
			for (i = 0; i < dynVec.length; i++)
			{
				
			}
			
			StopRecordingTime();
			
			//
			
			ToTrace("LoopOnVectorLength - cached length"); // 50 ms with 10M
			StartRecordingTime();
			
			for (i = 0; i < len; i++)
			{
				
			}
			
			StopRecordingTime();
		}
		
		private function NullCheck():void
		{
			ToTrace("null check != null");
			var mc:MovieClip = new MovieClip();
			
			StartRecordingTime();
			
			for (i = 0; i < len; i++)
			{
				if (mc != null)
				{
					
				}
			}
			
			StopRecordingTime();
			
			//
			
			ToTrace("null check if(value)");
			StartRecordingTime();
			
			for (i = 0; i < len; i++)
			{
				if (mc)
				{
					
				}
			}
			
			StopRecordingTime();
		}
		
		private function LoopWithIntUint():void
		{
			ToTrace("LoopWithIntUint - INT"); // 460 ms
			var loopInt:int = 100000000;
			var loopUint:uint = 100000000;
			StartRecordingTime();
			for (i = 0; i < loopInt; i++)
			{
				
			}
			
			StopRecordingTime();
			
			//
			
			ToTrace("LoopWithIntUint - UINT"); // 535 ms
			StartRecordingTime();
			
			for (i = 0; i < loopUint; i++)
			{
				
			}
			
			StopRecordingTime();
		}
		
		private function AccessTime():void
		{
			var value1:int = 64;
			var value2:int = 6;
			var value3:int = 0;
			var VAR_1_POS:uint = 0;
			var VAR_2_POS:uint = 4;
			var VAR_3_POS:uint = 8;
			
			ToTrace("normal access c=a+b");
			StartRecordingTime();
			for (i = 0; i < len; i++)
			{
				value3 = value1 + value2; // 55 ms
			}
			StopRecordingTime();
			
			//
			
			ToTrace("fastmem c=a+b");
			var byteArray:ByteArray = new ByteArray();
			byteArray.length = 1024;
			byteArray.position = 0;
			byteArray.writeInt(64);
			byteArray.writeInt(6);
			fastmem.fastSelectMem(byteArray);
			StartRecordingTime();
			for (i = 0; i < len; i++)
			{
				fastmem.fastSetI32(fastmem.fastGetI32(VAR_1_POS) + fastmem.fastGetI32(VAR_2_POS), VAR_3_POS); // 75 ms
			}
			StopRecordingTime();
			fastmem.fastDeselectMem();
			// 
			
			ToTrace("array c=a+b");
			var array:Array = [64, 4, 0];
			StartRecordingTime();
			for (i = 0; i < len; i++)
			{
				array[2] = array[0] + array[1]; // 280 ms
			}
			StopRecordingTime();
			
			//
			
			ToTrace("fixed vector c=a+b");
			var vec:Vector.<int> = new Vector.<int>(3, true);
			vec[0] = value1;
			vec[1] = value2;
			StartRecordingTime();
			for (i = 0; i < len; i++)
			{
				vec[2] = vec[0] + vec[1]; // 95 ms
			}
			StopRecordingTime();
		}
		
		private function DirectCall():void
		{
			
		}
		
		private function OnEvent(aEvent:Event):void
		{
			
		}
		
		private function DisplayResult():void
		{
			resultTxt = new TextField();
			resultTxt.height = stage.stageHeight;
			resultTxt.width = stage.stageWidth;
			addChild(resultTxt);
			
			resultTxt.text = resultString;
		}
		
		private function ToTrace(aText:String):void
		{
			resultString += aText + "\n";
		}
	}
}